package br.com.lp3.livraria.controller.login;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.lp3.livraria.dao.FuncionarioDao;
import br.com.lp3.livraria.model.Funcionario;

@WebServlet(urlPatterns="/view/login.do")
public class LoginServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String login = req.getParameter("inputEmail");
		String senha = req.getParameter("inputSenha");
		
		FuncionarioDao funcionarioDao = new FuncionarioDao();
		long id = funcionarioDao.login(login, senha);
		if (id > 0) {
			Funcionario funcionario = funcionarioDao.buscarPorId(id);
			HttpSession session = req.getSession();
			session.setAttribute("funcionario_session", funcionario);
			resp.sendRedirect("../index.jsp?login=1");
		} else {
			resp.sendRedirect("../index.jsp?fail=1");
		}
		
		
	}
}
